package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LeetCodeTest0530 {

    public static void main(String[] args) {

    }

    private ArrayList<Integer> result = new ArrayList<>();

    public int getMinimumDifference(TreeNode root) {
        int minDiff = Integer.MAX_VALUE;
        List<Integer> result = inorderTraversal(root);
        for (int i = 0; i < result.size() - 1; i++) {
            int diff = result.get(i + 1) - result.get(i);
            minDiff = Math.min(minDiff, diff);
        }
        return minDiff;
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        if (root == null) {
            return result;
        }
        Stack<TreeNode> treeNodeStack = new Stack<>();
        while (root != null || !treeNodeStack.isEmpty()) {
            if (root != null) {
                treeNodeStack.push(root);
                root = root.left;
            } else {
                root = treeNodeStack.pop();
                result.add(root.val);
                root = root.right;
            }
        }
        return result;
    }
}
